package com.radar.task.cd;

import cn.hutool.core.io.FileUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.Comparator;
import java.util.List;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
 * @author: fy-wbj
 * @description: radar task
 * @date: 2021-04-21
 */

@Component
@Slf4j
@Service(value = "FileRunnable")
public class FileRunnable {

//    @Scheduled(cron = "0 0/1 * * * ? ")

    /**
     * 覆盖代码注释
     */
    public void jobRun() {
        List<File> files = FileUtil.loopFiles(Constants.rootPath + File.separator).stream().filter(i -> Pattern.matches("^Z_RADR_I_Z(\\d{4})_(\\d{14})_O_DOR_(.*?)_CAP_FMT.(bin.bz2|bin)$", i.getName()))
                .sorted(Comparator.comparingLong(i -> Long.parseLong(i.getName().split("_")[4])))
                .collect(Collectors.toList());
        int index = 0;
        for (File file : files) {
            File OKFile = new File(Constants.tmpPath + File.separator + "OK" + File.separator + file.getName() + ".OK");
            if (OKFile.exists()) {
                continue;
            }
            if (file.exists()) {
                try {
                    Constants.singleQueue.put(file);
                    FileUtil.touch(OKFile);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    FileUtil.touch(OKFile);
                }
            }
        }
//        }
    }

    /**
     * 覆盖代码注释
     * @param args
     * @throws InterruptedException
     */
    public static void main(String[] args) throws InterruptedException {
        for(int i = 0 ; i < 4000 ; i ++){
            Constants.singleQueue.put(new File(i + ""));
        }
        System.out.println(Constants.singleQueue.size());
    }
}
